# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from osv import fields, osv, orm
import tools
import logging
import addons

class village_resident_move_out(orm.TransientModel):
    """迁出户籍"""
    _name = "village.resident.move.out"
    _description = "move resident out"
    
    _columns = {
        'resident_name': fields.char('迁出人姓名', size=50, required=True, readonly=True),
        'move_out_date': fields.date('迁出日期', required=True),
        'move_out_reason': fields.char('迁出原因', size=50, required=True),
    }
    
    _defaults = {
        'move_out_date': fields.date.context_today,
    }
    
    def default_get(self, cr, uid, fields, context=None):
        if context is None: context = {}
        # no call to super!
        res = {}
        resident_id = context.get('active_id', None)
        if not resident_id or not context.get('active_model') == 'village.resident':
            return res
        if 'resident_name' in fields:
            resident_id = self.pool.get('village.resident').browse(cr, uid, resident_id, context=context)
            res.update(resident_name = resident_id.name)
        return res
        
    def move_out(self, cr, uid, ids, context=None):
        resident_obj = self.pool.get('village.resident')
        data = self.read(cr, uid, ids, [])[0]
        
        
        resident_id = context.get('active_id', None)
        to_do_ids = [resident_id]
        
        resident = residen_obj.browse(cr, uid, resident_id, context)
        if resident.is_householder:
            for member in resident_obj.search(cr, uid, [('householder', '=', resident)]):
                to_do_ids.append(member.id)
                
        resident_obj.write(cr, uid, to_do_ids, 
                           {'move_out_reason': data.get('move_out_reason'), 
                            'move_out_date':data.get('move_out_date'), 
                            'state':'moved_out'})

        return {'type': 'ir.actions.act_window_close'}


# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
